

;==========
;=        =
;= CHAR2X =
;=        =
;==========
;
;implementation of EPX/Scale2x/AdvMAME2x
;
;for a single character
;
; char2x(HL = 8 bytes of char): HL = 32 bytes of scaled char
;

	.MODULE CHAR2X

_CELLB	.EQU
_CELLD
_CELLE
_CELLF
_CELLH
_CELLE0
_CELLE1
_CELLE2
_CELLE3

char2x
	;HL = char ptr (8 bytes)
	ld de, _char2xbufs
	ld bc, 8
	ldir			;copy char

	ld c, 8
_char2x_3
	ld b, 8
_char2x_2
	call _char2x_1
	djnz _char2x_2	
	dec c
	jr nz, _char2x_3
	ret

_char2x_1
	;scale one pixel
	push bc

	dec c			;getpixel [x,y-1] [x,y] [x,y+1] [x-1,y] [x+1,y]
	push bc
	call _getpixel
	ld (ix + _CELLB), a
	pop bc
	inc c
	push bc
	call _getpixel
	ld (ix + _CELLE), a
	pop bc
	push bc
	inc c
	call _getpixel
	ld (ix + _CELLH), a
	pop bc
	push bc
	dec b
	call _getpixel
	ld (ix + _CELLD), a
	pop bc
	inc b
	call _getpixel
	ld (ix + _CELLF), a
	
	call _algo
	pop bc
	push bc
	
	sla b
	sla c	
	

	pop bc
	ret



_algo
	;ALGO description
	;as taken from http://scale2x.sourceforge.net/algorithm.html
	;
	;
	; A B C
	; D E F ->  E0 E1
	; G H I     E2 E3
	;
	; 

	ld b, (ix + _CELLB)
	ld c, (ix + _CELLD)
	ld d, (ix + _CELLF)
	ld e, (ix + _CELLH)
	ld l, (ix + _CELLE)	

	;if (B != H && D != F) {

	ld a, b
	cp e
	jr z, _else
	ld a, c
	cp d
	jr z, _else
		;E0 = D == B ? D : E
		;ld a, c
		cp b
		jr z, _if2
		ld a, l
_if2
		ld (ix + _CELLE0), a
		;E1 = B == F ? F : E
		ld a, d
		cp b
		jr z, _if4
		ld a, l
_if4
		ld (ix + _CELLE1), a
		;E2 = D == H ? D : E 
		ld a, c
		cp e
		jr z, _if6
		ld a, l
_if6
		ld (ix + _CELLE2), a
		;E3 = H == F ? F : E
		ld a, d
		cp e
		jr z, _if7
		ld a, l
_if7
		ld (ix + _CELLE3), a
		jr _endif	
_else		
		ld (ix + _CELLE0), l
		ld (ix + _CELLE1), l
		ld (ix + _CELLE2), l
		ld (ix + _CELLE3), l
_endif
	;algo end
	ret





_setpixel
	;B = x
	;C = y


		




_getpixel
	;B = x
	;C = y
	;          IX = _char2xbufs
	ld a, c
	and 7
	ld (_getpoint1+2 ), a
	ld a, b
	and 7
	ld b, a
_getpoint1
	ld a, (ix + 0)
	ld c, %10000000
	inc b
	dec b
	jr z, _getpoint2
_getpoint3
	rrca
	djnz _getpoint3
_getpoint2
	and c
	ret z
	ld a, 255
	ret

_char2xbufs
	.WORD 0,0,0,0	;0-7 character

	.WORD 0,0,0,0	;8-39 result
	.WORD 0,0,0,0
	.WORD 0,0,0,0
	.WORD 0,0,0,0
			
	.BYTE 0,0,0,0	;1 2 3 4     40 41 42 43
	.BYTE 0,0,0,0,0 ;A B C D P   44 45 46 47 48
	
	
	